'use strict';
app.controller('noticeCtrl',['$scope','$modal','noticeService','utilService','toaster', function($scope,$modal,noticeService,utilService,toaster){

    $scope.page = {};
    $scope.page.currentPage = 1;
    //分页
    $scope.replacePage = function () {
        $scope.currentPage = $scope.page.currentPage;
        $scope.pageChanged();
    };

    // call server.
    $scope.query = function(tab){
        $scope.list=new Array();
        switch (tab){
            case 0:
                utilService.queryForPagingList($scope,{
                    url:$scope.app.domain + 'notice/news',
                }).then(function(result){
                });
                break;
            case 1:
                utilService.queryForPagingList($scope,{
                    url:$scope.app.domain + 'notice',
                }).then(function(result){
                });
                break;
        }
    }
    $scope.query(0);
//禁用启用
    $scope.changeStatus= function(id){
        $scope.notice = {
            id:id
        };
        noticeService.changeStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query(1);
                    break;
                case 625:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
    //使失效
    $scope.makeFailure= function(id){
        $scope.notice = {
            id:id
        };
        noticeService.makeFailure($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query(1);
                    break;
                case 625:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
//置顶
    $scope.toTop= function(id){
        $scope.notice = {
            id:id
        };
        noticeService.toTop($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query(1);
                    break;
                case 625:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
//删除公告
    $scope.openDelete = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/notice/notice_delete.html',
            controller: 'delNoticeCtrl',
            scope: $scope,
            size:'sm',
            resolve: {
                data: function () {
                    var data = {};
                    data.id = id;
                    data.name = name;
                    return data;
                }
            }
        });
    }
}]);
//新增公告controller
app.controller('addNoticeCtrl',['$scope','$state','$filter','noticeService','toaster', function($scope,$state,$filter,noticeService,toaster){
    $scope.notice={
        isApi:0,
        isShop:1,
        isCity:0
    }
    $scope.AllowToAdd=true;
    $scope.addNotice = function(){
        if(!$scope.AllowToAdd){
            return;
        }
        $scope.AllowToAdd=false;
        noticeService.addNotice($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.AllowToAdd=true;
                    $state.go('app.notice');
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.AllowToAdd=true;
                    break
            }
        });
    }

}]);
//修改公告Controller
app.controller('editNoticeCtrl',['$scope','$state','$stateParams','noticeService','toaster','$filter', function($scope,$state,$stateParams,noticeService,toaster,$filter){
    $scope.notice = {
        id:$stateParams.id
    };
    //根据id查询公告
    noticeService.findById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.notice = result.data;
                break;
            case 625:
                toaster.pop('waiting', "提示", result.message);
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });

    //修改公告
    $scope.AllowToEdit=true;
    $scope.editNotice = function(){
        if(!$scope.AllowToEdit){
            return;
        }
        $scope.AllowToEdit=false;
        var endTime =$filter('date')($scope.notice.endTime,'yyyy-MM-dd 00:00:00');
        $scope.notice.endTime = endTime;
        noticeService.editNotice($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.AllowToEdit=true;
                    $state.go('app.notice');
                    break;
                case 625:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.AllowToEdit=true;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.AllowToEdit=true;
                    break
            }
        });
    }

}]);

//修改公告Controller
app.controller('noticeDetailCtrl',['$scope','$state','$stateParams','noticeService','toaster','$filter', function($scope,$state,$stateParams,noticeService,toaster,$filter){
    $scope.notice = {
        id:$stateParams.id
    };
    //根据id查询公告
    noticeService.findById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.notice = result.data;
                break;
            case 625:
                toaster.pop('waiting', "提示", result.message);
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });
}]);

//删除模态框Controller
app.controller('delNoticeCtrl', ['$scope', '$modalInstance','noticeService','toaster','data',
    function($scope, $modalInstance,noticeService,toaster,data) {
        $scope.notice=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            noticeService.delNotice($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.AllowToDelete=true;
                        $scope.query(1);
                        break;
                    case 625:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

angular.module('CcxiApp', []);
app.filter('textLengthSet', function() {
    return function(value, wordwise, max, tail) {
        if (!value) return '';

        max = parseInt(max, 10);
        if (!max) return value;
        if (value.length <= max) return value;

        value = value.substr(0, max);
        if (wordwise) {
            var lastspace = value.lastIndexOf(' ');
            if (lastspace != -1) {
                value = value.substr(0, lastspace);
            }
        }

        return value + (tail || ' …');//'...'可以换成其它文字
    };
});